<template>
  <hi-row
    isColumn
    :wrap="false"
    class="viewcontainer"
    v-if="dataset && subDataSet"
    id="test"
  >
    <!-- {{dataset.isReadonly()}} -->
    <hi-col flex="300px">
      <hi-toolbar
        :source="dataset"
        v-bind="toolbar"
        ref="dbtoolbar"
        :addOne="true"
        :disabled="dataset.isReadonly()"
      >
      </hi-toolbar>
      <btn-toolbar @callFunc="clickBtn" class="btn-toolbar"></btn-toolbar>
      <!--        <el-button type="primary" icon="el-icon-printer" @click="clickPrint()">打印</el-button>-->
      <print
        :form-data="dataset.data[0]"
        :table-data="subDataSet.data"
        ref="print"
        v-if="dataset.data[0] && subDataSet.data"
      ></print>

      <hi-form
        :dataSource="dataset"
        ref="detailForm"
        :disabled="dataset.isReadonly()"
        :model="dataset.curRecord"
        v-bind="form"
      ></hi-form>
    </hi-col>
    <hi-col flex="auto" :min-height="200">
      <hi-table
        ref="detailTable"
        :dataSource="subDataSet"
        :isEdit="!subDataSet.isReadonly()"
        v-bind="dataTable"
        :model="subDataSet.data"
      ></hi-table>
    </hi-col>
  </hi-row>
</template>

<script>
import Print from "./print";
import BtnToolbar from "@/views/component/toolbar/index.vue";
import {
  HiForm,
  HiToolbar,
  HiTable,
  HiNumber,
  HiDataSelect,
  utils,
} from "hi-ui";
import viewBase from "../../base";
import toolbar from "@/hiuiDefaultConfig/toolbar";
import purchaseOrderConfig from "./dataset/purchaseOrder.js";
import purchaseOrderDetailConfig from "./dataset/purchaseOrderDetail.js";
import supplierCfg from "../../database/supplier/dataset";
import audittag from "@/views/component/audittag";
import drugCfg from "../../database/drug/dataset";

export default {
  name: "orderdetail",
  extends: viewBase,
  components: {
    HiForm,
    HiToolbar,
    HiTable,
    HiNumber,
    audittag,
    HiDataSelect,
    Print,
    BtnToolbar,
  },

  data() {
    return {
      //数据集
      dataset: null,
      //数据集配置
      datasetCfg: purchaseOrderConfig,
      //从表数据集
      subDataSet: null,
      //从表数据集配置
      subDatasetCfg: purchaseOrderDetailConfig,
      //数据桥梁
      dataBridge: new Map(),
      //数据表格
      dataTable: {
        hasSelection: true,
        hasRowNumber: true,
        showSummary: true,
        columns: [
          {
            name: "FGOODSNUMBER",
            width: 150,
            element: HiDataSelect /* "hi-data-select", */,
            props: {
              // 下拉查找枚举配置
              selectOption: {
                clearable: true,
                filterable: true,
                // "default-first-option": true,
              },
              lookup: {
                // 下拉查找枚举网格配置
                tableprops: {
                  "show-header": true,
                  white_space_nowrap: true,
                },
                // 下拉查找过滤字段
                filternames: [
                  {
                    name: "FNUMBER",
                  },
                ],
                // 下拉查找显示字段,默认用columns
                filterColumns: [
                  {
                    name: "FDESC",
                    label: "名称",
                    width: 180,
                  },
                  {
                    name: "FNUMBER",
                    label: "店内编码",
                    width: 180,
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                    width: 180,
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                    width: 180,
                  },
                ],
                storeCfg: drugCfg,
                active: true,
                multiSelect: true,
                addRecord: true,
                checkValid: true,
                target: {
                  id: "OrderDetailEntity",
                }, //目标
                source: {
                  id: "DrugEntity",
                  modelFile: "database/drug/dataset/DrugEntity.xml",
                }, //来源 //来源数据集查询条件
                /*   queryCdions: {
              FNUMBER: function (rec) {
                return rec["FNUMBER1"];
              },
            },  */ updateFields: [], //更新字段
                limitEqualsFields: [], //相同字段
                uniqueFields: ["FBARCODE"], //唯一字段
                returnFields: {
                  FGOODSNUMBER: "FNUMBER",
                  FBARCODE: "FBARCODE",
                  FDESC: "FDESC",
                  FBRAND: "FBRAND",
                  FUNIT: "FUNIT",
                  FPRICE: "FPRICE",
                  FSPEC: "FSPEC",
                  FRATE: "FENTERRATE",
                  FPROCOMPANY: "FPROCOMPANY",
                  FISRETAIL: "FISRETAIL",
                },
                columns: [
                  {
                    fixed: true,
                    name: "FDESC",
                    label: "通用名称",
                  },

                  {
                    name: "FNUMBER",
                    label: "店内编码",
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                  },
                  {
                    name: "FBRAND",
                    label: "品牌",
                  },
                  {
                    name: "FUNIT",
                    label: "单位",
                  },

                  {
                    name: "FPRICE",
                    label: "零售价",
                    formType: "dbDouble",
                    props: {
                      precision: 2,
                      format: "￥0.00",
                    },
                  },
                  {
                    name: "FENTERRATE",
                    label: "进项税率",
                    formType: "dbDouble",
                    props: {
                      format: "0.00%",
                    },
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "FDESC",
                      label: "通用名称",
                      element: "el-input",
                    },
                    {
                      name: "FNUMBER",
                      label: "店内编码",
                      element: "el-input",
                    },
                    {
                      name: "FBARCODE",
                      label: "条形码",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },
          {
            name: "FBARCODE",
            width: 150,
            element: HiDataSelect /* "hi-data-select", */,
            props: {
              // 下拉查找枚举配置
              selectOption: {
                clearable: true,
                filterable: true,
                // "default-first-option": true,
              },
              lookup: {
                // 下拉查找枚举网格配置
                tableprops: {
                  "show-header": true,
                  white_space_nowrap: true,
                },
                // 下拉查找过滤字段
                filternames: [
                  {
                    name: "FBARCODE",
                  },
                ],
                // 下拉查找显示字段,默认用columns
                filterColumns: [
                  {
                    name: "FDESC",
                    label: "名称",
                    width: 180,
                  },
                  {
                    name: "FNUMBER",
                    label: "店内编码",
                    width: 180,
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                    width: 180,
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                    width: 180,
                  },
                ],
                storeCfg: drugCfg,
                active: true,
                multiSelect: true,
                addRecord: true,
                checkValid: true,
                target: {
                  id: "OrderDetailEntity",
                }, //目标
                source: {
                  id: "DrugEntity",
                  modelFile: "database/drug/dataset/DrugEntity.xml",
                }, //来源 //来源数据集查询条件
                /*   queryCdions: {
              FNUMBER: function (rec) {
                return rec["FNUMBER1"];
              },
            },  */ updateFields: [], //更新字段
                limitEqualsFields: [], //相同字段
                uniqueFields: ["FBARCODE"], //唯一字段
                returnFields: {
                  FGOODSNUMBER: "FNUMBER",
                  FBARCODE: "FBARCODE",
                  FDESC: "FDESC",
                  FBRAND: "FBRAND",
                  FUNIT: "FUNIT",
                  FPRICE: "FPRICE",
                  FSPEC: "FSPEC",
                  FRATE: "FENTERRATE",
                  FPROCOMPANY: "FPROCOMPANY",
                  FISRETAIL: "FISRETAIL",
                },
                columns: [
                  {
                    fixed: true,
                    name: "FDESC",
                    label: "通用名称",
                  },

                  {
                    name: "FNUMBER",
                    label: "店内编码",
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                  },
                  {
                    name: "FBRAND",
                    label: "品牌",
                  },
                  {
                    name: "FUNIT",
                    label: "单位",
                  },

                  {
                    name: "FPRICE",
                    label: "零售价",
                    formType: "dbDouble",
                    props: {
                      precision: 2,
                      format: "￥0.00",
                    },
                  },
                  {
                    name: "FENTERRATE",
                    label: "进项税率",
                    formType: "dbDouble",
                    props: {
                      format: "0.00%",
                    },
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "FDESC",
                      label: "通用名称",
                      element: "el-input",
                    },
                    {
                      name: "FNUMBER",
                      label: "店内编码",
                      element: "el-input",
                    },
                    {
                      name: "FBARCODE",
                      label: "条形码",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },
          {
            width: "150px",
            name: "FDESC",
            element: HiDataSelect,
            props: {
              // 下拉查找枚举配置
              selectOption: {
                clearable: true,
                filterable: true,
                // "default-first-option": true,
              },
              lookup: {
                // 下拉查找枚举网格配置
                tableprops: {
                  "show-header": true,
                  white_space_nowrap: true,
                },
                // 下拉查找过滤字段
                filternames: [
                  {
                    name: "FDESC",
                  },
                  {
                    name: "FPINYIN",
                  },
                  {
                    name: "FJIANPIN",
                  },
                ],
                // 下拉查找显示字段,默认用columns
                filterColumns: [
                  {
                    name: "FDESC",
                    label: "名称",
                    width: 180,
                  },
                  {
                    name: "FNUMBER",
                    label: "店内编码",
                    width: 180,
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                    width: 180,
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                    width: 180,
                  },
                ],
                storeCfg: drugCfg,
                active: true,
                multiSelect: true,
                addRecord: true,
                checkValid: true,
                target: {
                  id: "OrderDetailEntity",
                }, //目标
                source: {
                  id: "DrugEntity",
                  modelFile: "database/drug/dataset/DrugEntity.xml",
                }, //来源 //来源数据集查询条件
                /*   queryCdions: {
              FNUMBER: function (rec) {
                return rec["FNUMBER1"];
              },
            },  */ updateFields: [], //更新字段
                limitEqualsFields: [], //相同字段
                uniqueFields: ["FBARCODE"], //唯一字段
                returnFields: {
                  FGOODSNUMBER: "FNUMBER",
                  FBARCODE: "FBARCODE",
                  FDESC: "FDESC",
                  FBRAND: "FBRAND",
                  FUNIT: "FUNIT",
                  FPRICE: "FPRICE",
                  FSPEC: "FSPEC",
                  FRATE: "FENTERRATE",
                  FPROCOMPANY: "FPROCOMPANY",
                  FISRETAIL: "FISRETAIL",
                },
                columns: [
                  {
                    fixed: true,
                    name: "FDESC",
                    label: "通用名称",
                  },

                  {
                    name: "FNUMBER",
                    label: "店内编码",
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                  },
                  {
                    name: "FBRAND",
                    label: "品牌",
                  },
                  {
                    name: "FUNIT",
                    label: "单位",
                  },

                  {
                    name: "FPRICE",
                    label: "零售价",
                    formType: "dbDouble",
                    props: {
                      precision: 2,
                      format: "￥0.00",
                    },
                  },
                  {
                    name: "FENTERRATE",
                    label: "进项税率",
                    formType: "dbDouble",
                    props: {
                      format: "0.00%",
                    },
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "FDESC",
                      label: "通用名称",
                      element: "el-input",
                    },
                    {
                      name: "FNUMBER",
                      label: "店内编码",
                      element: "el-input",
                    },
                    {
                      name: "FBARCODE",
                      label: "条形码",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },

          {
            name: "FBRAND",
          },
          {
            name: "FSPEC",
          },
          {
            name: "FUNIT",
          },
          {
            name: "FPROCOMPANY",
          },
          {
            name: "FAMOUNT",
            props: {
              min: 0,
              precision: 2,
            },
            summary: "sum",
          },

          {
            name: "FPRICE",
            props: {
              min: 0,
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FMONEY",
            props: {
              format: "￥0.00",
              precision: 2,
            },
            summary: "sum",
          },
          {
            name: "FRATE",
            props: {
              min: 0,
              format: "0.00%",
            },
          },
          {
            name: "FRATEMONEY",
            props: {
              format: "￥0.00",
              precision: 2,
            },
            summary: "sum",
          },
          {
            name: "FNOTE",
          },
        ],
      },
      toolbar: toolbar,
      //表单配置
      form: {
        columnsTotal: 2,
        items: [
          {
            name: "FNUMBER",
          },
          {
            name: "FSUPPLIER",
            element: "hi-data-select",
            props: {
              // 下拉查找枚举配置
              selectOption: {
                clearable: true,
                filterable: true,
                // "default-first-option": true,
              },
              lookup: {
                // 下拉查找枚举网格配置
                tableprops: {
                  "show-header": true,
                  white_space_nowrap: true,
                },
                // 下拉查找过滤字段
                filternames: [
                  {
                    name: "FDESC",
                  },
                  {
                    name: "FPINYIN",
                    // operate: "like",
                  },
                  {
                    name: "FJIANPIN",
                  },
                ],
                // 下拉查找显示字段,默认用columns
                filterColumns: [
                  {
                    name: "FDESC",
                    label: "供应商",
                    // width: 180,
                  },
                ],
                storeCfg: supplierCfg,
                active: true,
                /* treeOption:{
                                                    "modelFile":"users",
                                                    label: "name",
                                                    children: "company"
                                                }, */

                multiSelect: false,
                checkValid: true,
                returnFields: {
                  FSUPPLIER: "FDESC",
                  FSUPPLIERID: "FID",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "供应商名称",
                  },
                  {
                    name: "FENTERPRISEPERSON",
                    label: "企业法人",
                  },
                  {
                    name: "FCONTACTMAN",
                    label: "联系人",
                  },
                  {
                    name: "FCONTACTTEL",
                    label: "联系电话",
                  },
                  {
                    name: "FBUSINESSTYPE",
                    label: "经营类型",
                  },
                  {
                    name: "FCERTIFICATION",
                    label: "认证类别",
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "FDESC",
                      label: "供应商",
                      element: "el-input",
                    },
                    {
                      name: "FCONTACTMAN",
                      label: "联系人",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },
          /* {
            name: "FARRIVALSTATE",

             element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",
                param: {
                  FPID: "dhqk",
                },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FARRIVALSTATE: "FDESC",

                },
                columns: [
                  {
                    name: "FDESC",
                    label: "到货情况",
                  },
                ],
              },
            },
          }, */
          {
            name: "FNOTE",
            colspan: 2,
            props: {
              type: "textarea",
              autosize: {
                minRows: 4,
                maxRows: 4,
              },
              maxlength: 200,
            },
          },
          {
            name: "FUSERID",
          },
          {
            name: "FCREATEDATE",
          },
          {
            name: "FMONEY",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            element: audittag,
            name: "FAUDIT",
          },
        ],
      },
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    clickBtn(methodName) {
      console.log(methodName);
      switch (methodName) {
        case "print":
          this.$refs.print.onPrint();
          break;
      }
    },
    addDetail() {
      this.subDataSet.add({});
    },

    test() {
      debugger;
    },

    //更新金额
    updateFMONEY(data, store, options) {
      console.log("updateFMONEY", data);
      if (options.field == "FMONEY") {
        let allData = store.data,
          sum = 0;
        for (let i = 0; i < allData.length; i++) {
          let item = allData[i];
          sum = utils.number.accAdd(sum, item.FMONEY || 0);
        }
        this.dataset.data[0].FMONEY = sum;
      }
    },
    //视图初始化后
    viewInit() {
      let me = this;

      //从表注册修改后事件
      this.subDataSet.on("afterchange", (model, store, options) => {
        me.updateFMONEY(model.data, store, options);
      });

      if (this.$route.query.id) {
        this.dataset.load({
          FID: this.$route.query.id,
        });
      }
    },
  },
  created() {
    //给数据集配置添加别名
    let mainDatasetCfg = Object.assign({}, this.datasetCfg, {
      aliasName: "dataset",
    });
    let detailDatasetCfg = Object.assign({}, this.subDatasetCfg, {
      aliasName: "subDataSet",
    });
    //从数据集添加父数据集的ID
    detailDatasetCfg.relation.parentId = mainDatasetCfg.id;
    //加入数据集自动创建列表
    this.dsConfigs = [mainDatasetCfg, detailDatasetCfg];
    //设置数据集与组件的关系
    this.dataBridge.set("dataset", ["detailForm"]);
    this.dataBridge.set("subDataSet", ["detailTable"]);
    //流程绑数据集
    this.flowConfig = {
      dataset: "dataset",
    };
  },
  mounted() {
    // return;
    //创建主数据集
    this.dataset = this.createDataSet(this.datasetCfg); //this.$dsContainer.createDataSet(this.datasetCfg);
    this.dataset.on("dirtychange", (store, dirty) => {
      console.log("dataset" + dirty);
    });
    //创建从数据集
    this.subDataSet = this.createDataSet(this.subDatasetCfg, this.dataset);
    // this.subDataSet = this.$dsContainer.createDataSet(
    //   this.subDatasetCfg,
    //   this.dataset
    // );
    let me = this;
    this.subDataSet.on("afterchange", (model, store, options) => {
      me.updateFMONEY(model.data, store, options);
    });
    //设置数据与组件的关系
    this.dataBridge.set(this.dataset, ["detailForm", "detailTable"]); //, "detailTable"
    //流程信息
    this.flowConfig = {
      dataset: this.dataset,
    };
    if (this.$route.query.id) {
      this.dataset.load({
        FID: this.$route.query.id,
      });
    }
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scope>
.btn-toolbar {
  display: inline-block;
}
</style>
